<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width" />
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>首页Window</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>
        header {
            width: 100%;
            height: 50px;
            background-color: #e1017e;
        }

        header .left {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100px;
            height: 50px;
        }

        header .left .arrow {
            position: absolute;
            bottom: 21px;
            left: 11px;
            width: 13px;
            height: 8px;
            background: url('../image/arrow_down.png');
            background-size: 13px 8px;
            background-position: center center;
            ;
            background-repeat: no-repeat;
            -webkit-transition: 200ms;
            transition: 200ms;
        }

        header .left .arrow.active {
            -webkit-transform: rotate(180deg);
            transform: rotate(180deg);
        }

        header .left .city {
            position: relative;
            z-index: 2;
            width: 100%;
            height: 50px;
            padding-left: 27px;
            line-height: 50px;
            font-size: 14px;
            color: #fff;
        }

        header .center {
            width: 100%;
            height: 50px;
            background: url(../image/home_title.png);
            background-size: 74px 19px;
            background-position: center center;
            ;
            background-repeat: no-repeat;
        }

        header .right {
            position: absolute;
            bottom: 0;
            right: 0;
            width: 40px;
            height: 50px;
            background: url(../image/home_membercenter.png);
            background-size: 30px 30px;
            background-position: center center;
            ;
            background-repeat: no-repeat;
        }

        nav {
            display: -webkit-box;
            display: -webkit-flex;
            display: flex;
            -webkit-box-orient: horizontal;
            -webkit-flex-flow: row;
            flex-flow: row;
            position: relative;
            height: 40px;
            background-color: #e1017e;
        }

        nav .menu {
            -webkit-box-flex: 1;
            -webkit-flex: 1;
            flex: 1;
            height: 40px;
            line-height: 40px;
            font-size: 13px;
            color: #f973b8;
            text-align: center;
        }

        nav .menu.selected {
            font-size: 14px;
            color: #fff;
            font-weight: bolder;
        }

        span {
            padding: 5px 15px;
            margin: 10px;
            display: inline-block;
            background-color: #e8e8e8;
        }
    </style>
</head>

<body>
    <header id="header">
        <div class="left" tapmode onclick="fnOpenCitySelectorFrame()">
            <div class="arrow" id="arrow"></div>
            <div class="city" id="city">北京市</div>
        </div>
        <div class="center"></div>
        <div class="right" tapmodel onclick="fnOpenPersonalCenterWin()"></div>
    </header>

    <nav id="nav">
        <div class="menu selected" tapmode="selected" onclick="fnSetNavMenuIndex(0);">水果</div>
        <div class="menu" tapmode="selected" onclick="fnSetNavMenuIndex(1);">食材</div>
        <div class="menu" tapmode="selected" onclick="fnSetNavMenuIndex(2);">零食</div>
        <div class="menu" tapmode="selected" onclick="fnSetNavMenuIndex(3);">牛奶</div>
        <div class="menu" tapmode="selected" onclick="fnSetNavMenuIndex(4);">蔬菜8</div>
    </nav>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    apiready = function() {
        fnInitEventLisener();
        // 监听城市选择事件
        // api.addEventListener({
        //     name: 'citySelected'
        // }, function(ret, err) {
        //     $api.removeCls($api.byId("arrow"), 'active');
        //     $api.html($api.byId("city"), ret.value.cityName);
        //     api.closeFrame({
        //         name: 'citySelectorFrame'
        //     });
        // });

        var header = $api.byId('header');
        $api.fixStatusBar(header);
        var nav = $api.byId('nav');
        headerH = $api.offset(header).h;
        navH = $api.offset(nav).h;
        menus = $api.domAll($api.byId('nav'), '.menu');

        $api.rmStorage('currentCity');
        var currentCity = $api.getStorage('currentCity');
        if(!currentCity){
          fnOpenCitySelectorFrame();
          return;
        }

        openFrames();

        api.openFrame({
            name: 'minicart_frame',
            url: './minicart_frame.html',
            rect: {
                x: 0,
                y: api.winHeight - 55,
                w: 150,
                h: 34
            },
            bounces: false
        });

        api.bringFrameToFront({
            from: 'minicart_frame'
        });

    }

    var menus, headerH, navH;
    // 标签页点击事件触发函数
    function fnSetNavMenuIndex(index_) {
        // var menus = $api.domAll($api.byId("nav"), ".menu");
        // $api.addCls(menus[index_], 'selected');
        fnSetNavMenuSelected(index_);
        api.setFrameGroupIndex({
            name: 'mainFrameGroup',
            index: index_,
            scroll: true
        });
    };

    function fnSetNavMenuSelected(index_) {
        for (var i = 0; i < menus.length; i++) {
            if (index_ == i) {
                $api.addCls(menus[i], 'selected');
            } else {
                $api.removeCls(menus[i], 'selected');
            }
        }
    }

    // 点击个人中心头像 进入登录页
    function fnOpenPersonalCenterWin() {
        var userInfo = $api.getStorage('userInfo');
        if (userInfo) {
            api.openWin({
                name: 'personalcenter',
                url: './personalcenter.html',
                pageParam: {
                    userId: userInfo.userId
                }
            });
        } else {
            api.openWin({
                name: 'login',
                url: './login.html'
            });
        }
    }

    // 打开城市选择面板
    function fnOpenCitySelectorFrame() {
        var header = $api.byId("header");
        var headerH = $api.offset(header).h;
        api.openFrame({
            name: 'citySelectorFrame',
            url: './cityselector_frame.html',
            bgColor: 'rgba(0, 0, 0, 0.8)',
            rect: {
                x: 0,
                y: headerH,
                w: 'auto',
                h: 'auto'
            },
            bgColor: 'rgba(0,0,0,0.8)'
        });
        $api.addCls($api.byId("arrow"), "active");
    }

    function openFrames() {
      var frames = [];

      for (var i = 0; i < menus.length; i++) {
          frames.push({
              name: 'main_frame_' + i,
              url: './main_frame.html',
              overScrollMode:'always',
              pageParam: {
                  wareTypeIndex: i
              }
          });
      }
      // frames[0].pageParam.wareTypeId = "56c80da883af652643474b6b";
      // frames[1].pageParam.wareTypeId = "56c80db78d04c83d3d1dedea";
      // frames[2].pageParam.wareTypeId = "56c80dc031da9e480de1cb49";
      // frames[3].pageParam.wareTypeId = "56c80dc383af652643474b6d";
      // frames[4].pageParam.wareTypeId = "56c80dc88d04c83d3d1dedf3";

      api.openFrameGroup({
          name: 'mainFrameGroup',
          scrollEnabled: true,
          rect: {
              x: 0,
              y: headerH + navH,
              w: 'auto',
              h: 'auto'
          },
          index: 0,
          frames: frames,
          preload: frames.length
      }, function(ret, err) {
          fnSetNavMenuSelected(ret.index);
      });
    }

    function fnInitEventLisener() {
      api.addEventListener({
          name: 'cityChange'
      }, function(ret, err){
          if( ret ){
               openFrames();
          }else{
               alert( JSON.stringify( err ) );
          }
      });

      api.addEventListener({
          name: 'keyback'
      }, function(ret, err){
          api.confirm({
              title: '提示',
              msg: '是否退出应用',
              buttons: ['确定', '取消']
          }, function(ret, err){
              if(ret.buttonIndex == 1){
                api.closeWidget();

              }
          });

      });

    }
</script>

</html>
